home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / TSFAQP15.ZIP / FAQPAS.TXT / text0000.txt < prev   
Encoding:
Text File  |  1993-08-18  |  62.7 KB  |  1,337 lines

  1. FAQPAS.TXT Frequently (and not so frequently) asked Turbo Pascal
  2. questions with Timo's answers. The questions are in no particular
  3. order.
  4. Comments and corrections are solicited.
  5.  
  6. ..................................................................
  7. Prof. Timo Salmi      Co-moderator of comp.archives.msdos.announce
  8. Moderating  at  garbo.uwasa.fi anonymous FTP archives 128.214.87.1
  9. Faculty of Accounting & Industrial Management; University of Vaasa
  10. Internet:  ts@uwasa.fi  Bitnet:  salmi@finfun; FI-65101,   Finland
  11.  
  12. -------------------------------------------------------------------
  13.  1) How do I disable or capture the break key in Turbo Pascal?
  14.  2) How do I get a printed documentation of my students' TP runs?
  15.  3) What is the code for the weekday of a given date?
  16.  4) Need a program to format Turbo Pascal source code consistently
  17.  5) Can someone give me advice for writing a tsr program?
  18.  6) Why can't I read / write the com ports?
  19.  7) What are interrupts and how to use them in Turbo Pascal?
  20.  8) Should I upgrade my Turbo Pascal version?
  21.  9) How do I execute an MsDos command from within a TP program?
  22. 10) How is millisecond timing done?
  23. 11) How can I customize the text characters to my own liking?
  24. 12) How to find the files in a directory and subdirectories?
  25. 13) I need a power function but there is none in Turbo Pascal.
  26. 14) How can I create arrays that are larger than 64 kilobytes?
  27. 15) How can I test that the printer is ready?
  28. 16) How can I clear the keyboard type-ahead buffer.
  29. 17) How can I utilize expanded memory (EMS) in my programs?
  30. 18) How can I obtain the entire command line?
  31. 19) How do I redirect text from printer to file in my TP program?
  32. 20) Turbo Pascal is for wimps. Use standard Pascal or C instead?
  33. 21) How do I turn the cursor off?
  34. 22) How to find all roots of a polynomial?
  35. 23) What is all this talk about "Pascal homework on the net"?
  36. 24) How can I link graphics drivers directly into my executable?
  37. 25) How can I trap a runtime error?
  38. 26) How to get ansi control codes working in Turbo Pascal writes?
  39. 27) How to evaluate a function given as a string to the program?
  40. 28) How does one detect whether input (or output) is redirected?
  41. 29) How does one set the 43/50 line text mode?
  42. 30) How can I assign a value to an environment variable in TP?
  43. -------------------------------------------------------------------
  44.  
  45. Unless otherwise stated the answers cover versions 4.0, 5.0, 5.5,
  46. 6.0 and 7.0 (real mode). The Q&As are not for Turbo Pascal version 3
  47. or earlier. Objects, TVision, or Windows are not covered. (I do not
  48. use them myself.)
  49.  
  50. From ts@uwasa.fi Wed Aug 18 00:00:01 1993
  51. Subject: Disabling or capturing the break key
  52.  
  53. 1. *****
  54.  Q: I don't want the Break key to be able to interrupt my TP
  55. programs. How is this done?
  56.  Q2: I want to be able to capture the Break key in my TP program.
  57. How is this done?
  58.  Q3: How do I detect if a certain key has been pressed?
  59.  
  60.  A: This very frequently asked question is basically a case of RTFM
  61. (read the f*ing manual). But this feature is, admittedly, not very
  62. prominently displayed in the Turbo Pascal reference. (As a general
  63. rule we should not use the newsgroups as a replacement for our
  64. possibly missing manuals, but enough of this line.)
  65.    There is a CheckBreak variable in the Crt unit, which is true by
  66. default. To turn if off use
  67.      uses Crt;
  68.      :
  69.      CheckBreak := false;
  70.      :
  71. Besides turning off break checking this enables you to capture the
  72. pressing of the break key as you would capture pressing ctrl-c. In
  73. other words you can use e.g.
  74.      :
  75. procedure TEST;
  76. var key : char;
  77. begin
  78.   repeat
  79.     if KeyPressed then
  80.       begin
  81.         key := ReadKey;
  82.         case key of
  83.           #3 : begin writeln ('Break'); exit; end;  {ctrl-c or break}
  84.           else write (ord(key), ' ');
  85.         end; {case}
  86.       end; {if}
  87.   until false;
  88. end;  (* test *)
  89.      :
  90. IMPORTANT: Don't test the ctrl-break feature just from within the TP
  91. IDE, because it has ctlr-break handler ("intercepter") of its own
  92. and may confuse you into thinking that ctrl-break cannot be
  93. circumvented by the method given above.
  94.   The above example has a double purpose. It also shows the
  95. rudiments how you can detect if a certain key has been pressed. This
  96. enables you to give input without echoing it to the screen, which is
  97. a later FAQ in this collection.
  98.   This is, however, not all there can be to break checking, since
  99. the capturing is possible only at input time. It is also possible to
  100. write a break handler to interrupt a TP program at any time. For
  101. more details see Ohlsen & Stoker, Turbo Pascal Advanced Techniques,
  102. Chapter 7.
  103. --------------------------------------------------------------------
  104.  
  105. From ts@uwasa.fi Wed Aug 18 00:00:02 1993
  106. Subject: Directing output also to printer
  107.  
  108. 2. *****
  109.  Q: I want to have a printed documentation of my students' Turbo
  110. Pascal program exercises. How is all input and output directed also
  111. to the printer?
  112.  
  113.  A1: Use a screen capturing program to put everything that comes
  114. onto the screen into a file, and print the file. See FAQPROGS.TXT in
  115. /pc/ts/tsfaqn37.zip (or whatever version number is the current) for
  116. more about these programs. Available by anonymous FTP or mail server
  117. from garbo.uwasa.fi.
  118.  
  119.  A2: See the code in TSPAS.NWS (item: Redirecting writes to the
  120. printer) in the /pc/ts/tspa33??.zip (or whatever is the current
  121. version number) Turbo Pascal units package (?? = 40, 50, 55, 60, or
  122. 70 depending on your TP version). Alternatively use USECON and
  123. USEPRN routines in the TSUNTG unit of the same package.
  124.  
  125.      +------------------------------------------+
  126.      ! To get these and other packages given as !
  127.      !   /dir/subdir/name                       !
  128.      ! see the instructions in PD2ANS.TXT       !
  129.      +------------------------------------------+
  130.  
  131.  A3: But the really elegant solution to the problem of getting a
  132. logfile (or a printed list) of a Turbo Pascal run is to rewrite the
  133. write(ln) and read(ln) device driver functions. In itself writing
  134. such driver redirections is very advanced Turbo Pascal programming,
  135. but when the programming has once been done, the system is extremely
  136. easy to use as many times as you like. It goes like this. The driver
  137. redirections are programmed into a unit (say, tpulog or tpuprn). All
  138. that is needed after that is to include the following uses statement
  139. into the program (the target program) which has to be logged:
  140.       uses TPULOG;    ( or )    uses TPUPRN;
  141. This is all there is to it. Just adding one simple line to the
  142. target program. (If you call any other units, "uses tpulog" must
  143. come AFTER the system units (eg Dos), but BEFORE any which you may
  144. define yourself!)
  145.    The reason that I have named two units here instead of just one
  146. in the above example is that the preferred log for the target
  147. program may be a logfile or the printer. The better solution of
  148. these two is to use the logfile option, and then print it. The
  149. reason is simple. If the target program itself prints something,
  150. your printout will look confused.
  151.    The logging also has obvious limitations. It works for standard
  152. input and output (read(ln) and write(ln)) only. 1) It does not
  153. support graphics, in other words it is for the textmode. 2) It does
  154. not support direct (Crt) screen writes. 3) And, naturally it only
  155. shows the input and output that comes to the screen. Not any other
  156. input or output, such as from or to a file. 4) Furthermore, you are
  157. not allowed to reassign input or output. Statements like assign
  158. (output, '') will result in a crash, because the rewritten output
  159. device redirections are invalidated by such statements. 5) The
  160. device on the default drive must not be write protected, since else
  161. the logfile cannot be written to it. 6) It does not work for Turbo
  162. Pascal 4.0. Despite these restrictions, the method is perfectly
  163. suited for logging students' Turbo Pascal escapades.
  164.    It is advisable first to test and run your target program without
  165. "tpulog", so that if you get any strange errors you'll know whether
  166. they are caused by the logging.
  167.    Where to get such a unit. The code can be found in Michael
  168. Tischer (1990), Turbo Pascal Internals, Abacus, Section 4.2. Next a
  169. few of my own tips on this unit Tischer calls Prot. 1) The code is
  170. in incorrect order. The code that is listed on pages 142 - 145 goes
  171. between pages 139 and 140. 2) You can change the logfile name (const
  172. prot_name) to lpt1 for a printed list of the target program run. In
  173. that case it is advisable to include a test for the online status of
  174. the printer within Tischer's unit. 3) I see no reason why the two
  175. lines in Tischer's interface section couldn't be transferred to the
  176. implementation section. Why have any global definitions?  But all in
  177. all, it works like magic!
  178.  
  179.  A4: From: abcscnuk@csunb.csun.edu (Naoto Kimura (ACM))
  180. Subject: Re: Printing a log of students' exercises revisited
  181. To: ts@uwasa.fi
  182. Date: Fri, 2 Nov 90 20:52:03 pdt
  183. [Reproduced with Naoto's kind permission]
  184. By the way, several months ago, I had submitted a file (nktools.zip)
  185. file on Simtel20 that contains sources to a unit (LOGGER), which
  186. allows logging of I/O going through the standard input and output
  187. files, while still being able to use the program interactively.  I
  188. believe that I also submitted a copy to your site.  It was something
  189. I put together for use by students here at California State
  190. University at Northridge.  The source works equally well in all
  191. presently available versions of Turbo Pascal.
  192. The only requirements are that
  193.  * you place it as one of the last entries in the USES clause.  If
  194.    there is anything that redirects the standard input and output
  195.    file variables, you should put that unit before my unit in the
  196.    USES clause, so that it can see the I/O stream.
  197.  * Don't use the GotoXY and similar screen display control
  198.    procedures in the Crt unit and expect it to come out the same way
  199.    you had it on the display.  Since all my unit does is just
  200.    capture the I/O stream to send it through the normal channels and
  201.    also to the log file, all screen control information is not sent
  202.    to the log file.
  203.  * All I/O you want logged should go through the standard input and
  204.    output file variables.
  205.  * Don't close the standard input and output file variables, because
  206.    it will cause problems.  Basically, as far as I have checked, it
  207.    just causes the logging to stop at that point.
  208. --------------------------------------------------------------------
  209.  
  210. From ts@uwasa.fi Wed Aug 18 00:00:03 1993
  211. Subject: Code to give the weekday of a date
  212.  
  213. 3. *****
  214.  Q: I want code that gives the weekday of the given date.
  215.  
  216.  A1: There is a WKDAYFN function in /pc/ts/tspa33*.zip (or whatever
  217. version number is the latest, and where * is 40 50 55 60 and 70)
  218. Turbo Pascal units collection to give the modern weekday based on
  219. Zeller's congruence. Available by anonymous FTP or mail server from
  220. garbo.uwasa.fi. Also you can find a more extensive Julian and
  221. Gregorian weekday algorithm with source code in Dr.Dobb's Journal,
  222. June 1989, p. 148. Furthermore Press & Flannery & al (1986),
  223. Numerical Recipes, Cambridge University Press, present a weekday
  224. code. The Numerical Recipes codes are available as
  225. /pc/turbopas/nrpas13.zip (big, 404k!).
  226.  
  227.  A2: Some will recommend the following kludge. Store the current
  228. date, change it, and let MsDos get you the weekday. Don't use it! It
  229. is a bad suggestion. On top of being sloppy programming, there are
  230. several snags.  The trick works only for years 1980-2079. A crash
  231. the program may leave the clock at a wrong date. And even if
  232. multitasking is rare, in a multitasking environment havoc may result
  233. for the other tasks. And you may have a TSR that requires the
  234. correct date, etc.
  235. --------------------------------------------------------------------
  236.  
  237. From ts@uwasa.fi Wed Aug 18 00:00:04 1993
  238. Subject: Pretty printers (or uniform code)
  239.  
  240. 4. *****
  241.  Q: Where can I find a program that formats my (or my students')
  242. Turbo Pascal code in a consistent matter.
  243.  
  244.  A: What you are asking for is often called "a pretty printer".
  245. TurboPower Software's (the usual disclaimer applies) commercial
  246. Turbo Analyst has a facility for this with many options. There are
  247. also PD and shareware pretty printers, such as /pc/turbopas/
  248. pritprn.zip, bp7sb*.zip "Source Beautifier for Borland Pascal by
  249. J.Ferincz", and others at garbo.uwasa.fi available by anonymous FTP
  250. or mail server. See /pc/INDEX.ZIP for a list of the files.
  251. --------------------------------------------------------------------
  252.  
  253. From ts@uwasa.fi Wed Aug 18 00:00:05 1993
  254. Subject: How to write TSR programs
  255.  
  256. 5. *****
  257.  Q: Can someone give me advice for writing a tsr program.
  258.  
  259.  A: Writing a terminate and stay resident program can be considered
  260. advanced programming and is beyond the scope of an electronic
  261. message with limited space. Instead, here are some references to
  262. Turbo Pascal books and papers which have a coverage of the subject.
  263. Stephen O'Brien, Turbo Pascal, The Complete Reference, Chapter 16;
  264. Stephen O'Brien, Turbo Pascal, Advanced Programmer's Guide, Chapter
  265. 6; Michael Tischer, Turbo Pascal Internals, Chapter 11 (a definite
  266. bible of TP programming!); Michael Yester, Using Turbo Pascal,
  267. Chapter 19; Kent Pottebaum, "Creating TSR Programs with Turbo
  268. Pascal", Dr. Dobb's Journal, May 1989 and June 1989; Kris Jamsa, Dos
  269. Power User's Guide, pp. 649-.
  270. --------------------------------------------------------------------
  271.  
  272. From ts@uwasa.fi Wed Aug 18 00:00:06 1993
  273. Subject: Programming com ports
  274.  
  275. 6. *****
  276.  Q: Why can't I read / write the com ports.
  277.  
  278.  A: Com port programming (most often writing telecommunication
  279. programs) is much much more complicated than simply trying to use
  280.   write (com, whatever);
  281.   read  (com, whatever);
  282. This is a very advanced subject (frankly, beyond me), and the best
  283. way to learn is to try to obtain some code to show you how. One
  284. place to look at is Turbo Pascal text-books (I have a long list of
  285. them at garbo.uwasa.fi archives in /pc/ts/tsfaqp*.zip. There also is
  286. an example by David Rind in garbo.uwasa.fi/pc/pd2/faquote.zip.
  287. Another source is International FidoNet pascal conference at some
  288. bulletin board near you. The conference has had some very good
  289. discussions in it.  (No, I don't have them stored for distribution,
  290. nor any further information.)
  291. --------------------------------------------------------------------
  292.  
  293. From ts@uwasa.fi Wed Aug 18 00:00:07 1993
  294. Subject: Primers to interrupt programming
  295.  
  296. 7. *****
  297.  Q: What are interrupts and how to use them in Turbo Pascal?
  298.  
  299.  A: An interrupt is a signal to the processor from a program, a
  300. hardware device, or the processor itself, to suspend temporarily
  301. what the program is doing, and to perform a routine that is stored
  302. in the operating system. There are 256 such interrupt routines, with
  303. many subservices stored in memory at locations, which are given in
  304. the so called interrupt table. Turbo Pascal (somewhat like C) has a
  305. special keyword Intr, and a predefined variable registers (in the
  306. Dos unit) to access these interrupt routines. One way of looking at
  307. them is as Turbo Pascal (complicated lowlevel) subroutines that are
  308. already there ready for you to call.
  309.    A detailed description of interrupt routines is way beyond a
  310. single message with limited space. Instead, I shall give a simple
  311. example, and good references to the subject. (For a somewhat more
  312. comprehensive description of what an interrupt is, see INTERRUP.PRI
  313. in Ralf Brown's garbo.uwasa.fi:/pc/programming/inter35b.zip.)
  314.      :
  315.      uses Dos;
  316.      (* This procedure turns on the border color for CGA and VGA *)
  317.      procedure BORDER (color : byte);
  318.      var regs : registers;  (* Predeclared in the Dos unit *)
  319.      begin
  320.        FillChar (regs, SizeOf(regs), 0);  (* A precaution *)
  321.        regs.ax := $0B00;    (* Service number *)
  322.        regs.bh := $00;      (* Subservice number *)
  323.        regs.bl := color;
  324.        Intr ($10, regs);    (* ROM BIOS video driver interrupt *)
  325.      end;  (* border *)
  326.    If you are new the subject and / or want ideas on the most useful
  327. interrupts in Turbo Pascal programming, Ben Ezzel (1989),
  328. Programming the IBM User Interface Using Turbo Pascal, is definitely
  329. the best reference to look at. There are also many other good
  330. references for a novice interrupt user, such as Jamsa & Nameroff,
  331. Turbo Pascal Programmer's Library.
  332.    If you are a more advanced interrupt user you'll find the
  333. following references very useful. Michael Tischer (1990), Turbo
  334. Pascal Internals; Norton & Wilton (1988), The New Peter Norton
  335. Programmer's guide to the IBM PC & PS/2; Ray Duncan (1988), Advanced
  336. MS-DOS Programming; Terry Dettmann (1989), Dos Programmer's
  337. Reference, Second edition, Que. Furthermore, there is an impressive
  338. list of interrupts collected and maintained by Ralf Brown. His
  339. extensive /pc/programming/inter35a.zip, inter35b.zip and
  340. inter35c.zip (or whatever are the current versions when you read
  341. this) is available by anonymous FTP or mail server from
  342. garbo.uwasa.fi. A definite must for an advanced user. Also see the
  343. reference to Brown's and Kyle's book in the bibliography at the end
  344. of this FAQ. Another useful (but in many respects a replicate) list
  345. is contained in the file /pc/programming/dosref22.zip (or, sigh,
  346. whatever is the current version). There is also a good hypertext
  347. advanced programmer's quick reference /pc/programming/helppc21.zip
  348. which you might find useful.
  349.    One more point for Turbo Pascal users. When Borland upgraded from
  350. version 3 to 4.0 quite a number of tasks that needed to be done
  351. using interrupts (such as getting the current time) were included as
  352. normal TP routines. This means that while definitely useful,
  353. interrupt programming is now relevant only in advanced Turbo Pascal
  354. programming. Turbo Pascal 5.0 introduced a few more, but you can
  355. find some of the missing TP 4.0 routines in the compatibility unit
  356. in my garbo.uwasa.fi:/pc/ts/tspa3340.zip TP units collection.
  357. --------------------------------------------------------------------
  358.  
  359. From ts@uwasa.fi Wed Aug 18 00:00:08 1993
  360. Subject: Borland's Turbo Pascal upgrades
  361.  
  362. 8. *****
  363.  Q: Should I upgrade my Turbo Pascal version?
  364.  
  365.  A1: Depends on what version you are using, and for what purposes.
  366. If you are using version 3, the answer is a definite yes. There are
  367. so many useful additions in the later version, including the concept
  368. of units, and a great number of new useful keywords. The only reason
  369. that I can think of for using TP 3 is that it makes .com files
  370. (which reside in one memory segment only) instead of .exe files. As
  371. an accounting and business finance teacher and researcher I've been
  372. somewhat surprised to see postings stating that some users still
  373. have to program in TP 3.0 because their employer doesn't want to
  374. take the cost of upgrading. I find this cost argument ridiculous.
  375. How about some consideration for cost effectiveness and
  376. productivity?
  377.    If you are currently using version 4.0, the most important point
  378. in considering upgrading is the integrated debugger in the later
  379. versions. It is really good, and useful if you write much code.
  380. There are some minor considerations, as well. Later versions contain
  381. some useful routines which 4.0 does not. I have programmed many of
  382. them to be available also for 4.0 in my /pc/ts/tspa3340.zip TO units
  383. collection (or whatever is the latest when you read this).
  384. Furthermore, I find somewhat annoying that the executables will
  385. always end up in the default directory.
  386.    If you are currently using version 5.0 the rational reasons for
  387. upgrading are needing objects, and a better overlay manager. I have
  388. also version 5.5 myself, but switched back to version 5.0 after I
  389. had some problems with its linking of object files. (This is a false
  390. statement from me, since it turned out that I had made a mistake
  391. myself. My thanks are due to bj_stedm@gould2.bristol-poly.ac.uk
  392. (Bruce Stedman) for questioning this item). Anyway, I don't use nor
  393. need OOP objects (don't confuse linking object files and object
  394. oriented programming here). One further point for 5.5. It has a
  395. better help function than 5.0, and a few more procedures and
  396. predefined constants. The TP 5.5 help includes examples, which can
  397. be even pasted into your program. This is handy.
  398.    The real snag in upgrading (waiving the reasonable cost) is the
  399. fact that the units of the different versions are incompatible. If
  400. you have a large library of units (as I do) you will have to
  401. recompile the lot. This is something that has caused a fair amount
  402. of justifiable flak against an otherwise excellent product.
  403.    A tip. Don't throw away your Turbo Pascal version 3.0 manual, if
  404. you have one. It is of use if you resort to the Turbo3 and Graph3
  405. compatibility units. They give you access e.g. to turtle graphics.
  406.    At the time of first writing this Turbo Pascal 6.0 version had
  407. just been announced. I didn't have it yet myself, but I had been
  408. (correctly) informed that its units are not compatible with the
  409. earlier versions. I now have Turbo Pascal 6.0, and I must say that
  410. my reactions have been disappointment and frustration. This is
  411. probably partly (but not entirely) my own fault, since Turbo Pascal
  412. seems to be headed from a common programming language into a full
  413. professional's specialized tool, with many features I don't know how
  414. to utilize. The only advancement from my point of view really is the
  415. multiple file editing, but I have long had alternative programs for
  416. that. If I used assembler (I don't) I am sure that I would find
  417. useful TP 6.0's potential to include assembler code as such instead
  418. of having to use the cumbersome Inline procedure of entering the
  419. assembler code.
  420.    There is also a Windows Turbo Pascal, as the latest addition to
  421. the plethora. Since I don't use Windows at all, I have no futher
  422. information on it.
  423.    I think a pattern is emerging here. Rather than being different
  424. versions of the same product, the consecutive Turbo Pascals are
  425. really different products for different purposes. Version 3.0 was a
  426. simple programming language. Version 4.0 extended it into a full
  427. scale programming modular platform. Version 5.0 introduced the
  428. debugger. And there an advanced hobbyist's path ended. Version 5.5
  429. introduced object oriented programming, which I'm sure is important
  430. for the initiated, but personally I just don't need it even if I
  431. write a lot of programs. And with the 6.0 we go completely out of
  432. the realm of conventional programming into Turbo Pascal visions.
  433. And Windows Turbo Pascal is for a different platform, altogether.
  434.    I find the new integrated user interface of TP 6.0 awkward in
  435. comparison to what was used in the 4.0, 5.0, and 5.5 versions. The
  436. IDE of TP leaves less free memory than the previous versions.
  437. Furthermore TP 6.0 IDE performs frequent disk accesses which cause
  438. slowdowns  making it virtually unusable from a floppy. And I
  439. wonder why Borland didn't at once go all the way to Windows, because
  440. that is what 6.0 really is. An intermediate, incomplete step in that
  441. direction. This means that we have a 5th upgrade in line with
  442. incompatible units. This is aggravating even for a TP fan, isn't it?
  443.    For information on Turbo Pascal version 7.0 and Borland email
  444. contact numbers see garbo.uwasa.fi:/pc/turbopas/bp7-info.zip. Also
  445. see bp7bugs*.zip by Duncan Murdoch. Turbo Pascal 7.0 or more
  446. extensively Borland Pascal 7.0 is a full professional's tool, and
  447. far beyond for example my moderate programming needs. To list only a
  448. few of the features are protected mode programming, handling of
  449. lagre programs, very fast compiling, and a daunting amount of
  450. matrial elboving away on one's disk space if one ever has the
  451. patience to look through it all. I would use the word
  452. "overwhelming". But for a serious programmer this is an impressive
  453. and a very worthwhile tool. One should not be misled skipping it
  454. because of my comments which were written from a hobbyist's point of
  455. view. As a general trend in programs, the well-known columnist John
  456. C. Dvorak calls this increasing product complexity trend "featurism"
  457. in PC Computing, May 1993.
  458.  
  459.  A2: From: dmurdoch@watstat.waterloo.edu (Duncan Murdoch),
  460. Newsgroups: comp.lang.pascal. Included with Duncan's kind
  461. permission. (Duncan is one of the most knowledgeable and useful
  462. contributors to the comp.lang.pascal UseNet newsgroup).
  463.    One other reason:  there's a bug in the code generator for 4.0
  464. and 5.0 that makes it handle the Extended (10 byte) real type
  465. poorly.  The code generated makes very poor use of the 8 element
  466. internal stack on the coprocessor, so that expressions with lots of
  467. operands like
  468.   e1+e2+e3+e4+e5+e6+e7+e8+e9
  469. always fail, if all the e's are of type extended.  (The generated
  470. code pushes each operand onto the stack, then does all the adds.
  471. It's smarter to push and add them one at a time.)
  472.    This makes it a real pain translating numerical routines from
  473. Fortran, especially since constants are taken to be of type
  474. extended.
  475.    The bug was fixed in 5.5.
  476.  
  477.  A3: From: Bengt Oehman (d92bo@efd.lth.se): A difference between
  478. v4.0 and v5.5 is that you can calculate constants in tp55, but not
  479. in 4.0. I see this as a big advantage. For example:
  480.   CONST MaxW = 10;
  481.         MaxH = 20;
  482.         MaxSize = MaxW*MaxH;
  483.         { or }
  484.         MaxX = 100;
  485.         HalfMaxX = MaxX DIV 2;
  486. cannot be compiled with 4.0.
  487. --------------------------------------------------------------------
  488.  
  489. From ts@uwasa.fi Wed Aug 18 00:00:09 1993
  490. Subject: Shelling from a TP program
  491.  
  492. 9. *****
  493.  Q: How do I execute an MsDos command from within a TP program?
  494.  
  495.  A: The best way to answer this question is to give an example.
  496.      {$M 2048, 0, 0}   (* <-- Important *)
  497.      program outside;
  498.      uses dos;
  499.      begin
  500.        write ('Directory call from within TP by Timo Salmi');
  501.        SwapVectors;
  502.        Exec (GetEnv('comspec'), '/c dir *.*');  (* Execution *)
  503.        SwapVectors;
  504.        (* Testing for errors is recommended *)
  505.        if DosError <> 0 then
  506.          writeln ('Dos error number ', DosError)
  507.        else
  508.          writeln ('Mission accomplished, exit code ', DosExitCode);
  509.        (* For DosError and DosExitCode details see the TP manual *)
  510.      end.
  511. Alternatively, take a look at execdemo.pas from demos.arc which
  512. is on the accompanying Turbo Pascal disk.
  513.    What the above Exec does is that it executes the command
  514. processor. The /c specifies that the command interpreter is to
  515. perform the command, and then stop (not halt).
  516.    I have also seen it asked how one can swap the Turbo Pascal
  517. program to the disk when shelling. It is unnecessary to program that
  518. separately because there is an excellent program to do that for you.
  519. It is garbo.uwasa.fi:/pc/sysutil/shroom2d.zip.
  520. --------------------------------------------------------------------
  521.  
  522. From ts@uwasa.fi Wed Aug 18 00:00:10 1993
  523. Subject: Millisocond timing
  524.  
  525. 10. *****
  526.  Q: How is millisecond timing done?
  527.  
  528.  A: A difficult task, but the facilities are readily available.
  529. TurboPower Software's commercial Turbo Professional (don't confuse
  530. with Borland's Turbo Professional) has a unit for this. (The usual
  531. disclaimer applies). This one has been released to the PD. It is
  532. called tptimer and is part of the /pc/turbopas/bonus507.zip package.
  533. I have also seen a SIMTEL20 upload announcement of a ztimer11.zip
  534. for C and ASM, but I have no further information on that. Another
  535. option is /pc/turbopas/qwktimer.zip. It is not quite as accurate as
  536. tptimer.
  537.    To test the tptimer unit in bonus507.zip you can use the
  538. following example code
  539.   uses Crt, tptimer;
  540.   var time1, time2 : longint;
  541.   begin
  542.     InitializeTimer;
  543.     time1 := ReadTimer;
  544.     Delay (1356);    (* Or whatever code you wish to benchmark *)
  545.     time2 := ReadTimer;
  546.     RestoreTimer;
  547.     writeln ('Elapsed = ', ElapsedTime (time1, time2)/1000.0 : 0 : 3);
  548.   end.
  549.    It is quite another question when you really need the millisecond
  550. timing. The most common purpose for millisecond timing is testing
  551. the efficiency of alternative procedures and functions, right? The
  552. way I compare mine is simple. I call the procedures or functions I
  553. want to compare for speed, say, a thousand times in a loop.  And
  554. test this for elapsed time. This way the normal resolution (18.2
  555. cycles per second) of the system clock becomes sufficient. This is
  556. accurate enough for the comparisons.
  557.      var elapsed : real; i : word;
  558.      elapsed := TIMERFN;  (* e.g. from /pc/ts/tspa3355.zip *)
  559.      for i := 1 to 1000 do YOURTEST;  (* Try out the alternatives *)
  560.      elapsed := TIMERFN - elapsed;
  561.      writeln ('Elapsed : ', elapsed : 0 : 2);
  562. Incidentally, if you want to make more elaborate evaluations of the
  563. efficiency of your code, Borland's Turbo Profiler is a useful tool.
  564. (The usual disclaimer naturally applies.)
  565. --------------------------------------------------------------------
  566.  
  567. From ts@uwasa.fi Wed Aug 18 00:00:11 1993
  568. Subject: Text font customizing
  569.  
  570. 11. *****
  571.  Q: How can I customize the text characters to my own liking?
  572.  
  573.  A: As far as I know, text-mode characters are hard-coded, and
  574. cannot be customized at all unless you have an EGA or VGA adapter.
  575. But you can always retrieve the bitmap information for the ascii
  576. characters from your PC.
  577.    The bitmap table for the lower part of the character set (0-127)
  578. starts at memory position $F000 and ends at $FA6E. The upper part is
  579. not at a fixed memory location. The pointer to the memory address of
  580. upper part of the ascii table (provided that graftabl has been
  581. loaded) is at an address $007C. One way of saying this is that the
  582. segment address of the upper part's memory location is at $007E, and
  583. its offset at $007C.
  584.    Going into more details is beyond the scope of this posting. If
  585. you want more information see Kent Porter (1987), Stretching Turbo
  586. Pascal, Chapter 12, and Kent Porter & Mike Floyd (1990), Stretching
  587. Turbo Pascal. Version 5.5. Revised Edition. Brady, Chapter 11.
  588.    If you are interested in a demonstration of utilizing the
  589. bitmapped character information (no source code available), take a
  590. look at the demo in the garbo.uwasa.fi anonymous FTP archives file
  591. /pc/ts/tsdemo15.zip (or whatever version number is current).
  592.    Turbo Pascal also supports what is called stroked fonts (the
  593. .chr) files which draw characters instead of bitmapping them. The
  594. user should be able to write one's own .chr definitions, but I have
  595. no experience nor information on how this can be done.
  596.    There is something called bgikit10.zip which has facilities for
  597. making fonts and adding graphics drivers. The problem is that I
  598. cannot make it publicly available, since I think that it is not PD.
  599. I am still missing the information. Unfortunately, it is not even
  600. the only case where I encountered the fact that Borland does not
  601. seem at all interested in the UseNet users' queries about the status
  602. and distributability of their material.
  603.    (From Leonard Erickson Leonard.Erickson@f51.n105.z1.fidonet.org
  604. Well, you can *also* do it if you have a Hercules Graphics Card Plus
  605. or Hercules InColor card (as far as I know, the only cards that
  606. implemented Hercules RamFont 'standard'). And you can modify the
  607. upper 128 characters on a CGA card. BTW, the RamFont cards are
  608. *nice* pity it appeared too late to become a standard. It's a *lot*
  609. more flexible than EGA/VGA fonts (I can have several *dozen* fonts
  610. resident).)
  611. --------------------------------------------------------------------
  612.  
  613. From ts@uwasa.fi Wed Aug 18 00:00:12 1993
  614. Subject: Finding files in TP
  615.  
  616. 12. *****
  617.  Q: How to find the files in a directory AND subdirectories?
  618.  
  619.  A: Writing a program that goes through the files of the directory,
  620. and all the subdirectories below it, is based on Turbo Pascal's file
  621. finding commands and recursion. This is universal whether you are
  622. writing, for example, a directory listing program, or a program that
  623. deletes, say, all the .bak files, or some other similar task.
  624.    To find (for listing or other purposes) the files in a directory
  625. you need above all the FindFirst and FindNext keywords, and testing
  626. the predefined file attributes. You make these a procedure, and call
  627. it recursively. If you want good examples with source code, please
  628. see PC World, April 1989, p. 154; Kent Porter & Mike Floyd (1990),
  629. Stretching Turbo Pascal. Version 5.5. Revised Edition. Brady,
  630. Chapter 23; and Michael Yester (1989), Using Turbo Pascal, p. 437;
  631. dirtree.pas in /pc/pcmag/vol8n01.zip.
  632.    The simple (non-recursive) example listing all the read-only
  633. files in the current directory shows the rudiments of the principle
  634. of Using FindFirst, FindNext, and the file attributes, because some
  635. users find it hard to use these keywords.
  636.     uses Dos;
  637.     var FileInfo : SearchRec;
  638.     begin
  639.       FindFirst ('*.*', AnyFile, FileInfo);
  640.       while DosError = 0 do
  641.         begin
  642.           if (FileInfo.Attr and ReadOnly) > 0 then
  643.             writeln (FileInfo.Name);
  644.           FindNext (FileInfo);
  645.         end;
  646.     end.  (* test *)
  647.  
  648.  A2: While we are on the subject related to FindFirst and FindNext,
  649. here are two useful examples:
  650.  
  651. (* Number of files in a directory (not counting directories) *)
  652. function DFILESFN (dirName : string) : word;
  653. var nberOfFiles  : word;
  654.     FileInfo     : searchRec;
  655. begin
  656.   if dirName[Length(dirName)] <> '\' then dirName := dirName + '\';
  657.   dirName := dirName + '*.*';
  658.   {}
  659.   nberOfFiles := 0;
  660.   FindFirst (dirName, AnyFile, FileInfo);
  661.   while DosError = 0 do
  662.     begin
  663.       if ((FileInfo.Attr and VolumeId) = 0) then
  664.         if (FileInfo.Attr and Directory) = 0 then
  665.           Inc (nberOfFiles);
  666.       FindNext (FileInfo);
  667.     end; {while}
  668.   dfilesfn := nberOfFiles;
  669. end;  (* dfilesfn *)
  670.  
  671. (* Number of immediate subdirectories in a directory *)
  672. function DDIRSFN (dirName : string) : word;
  673. var nberOfDirs : word;
  674.     FileInfo    : searchRec;
  675. begin
  676.   if dirName[Length(dirName)] <> '\' then dirName := dirName + '\';
  677.   dirName := dirName + '*.*';
  678.   {}
  679.   nberOfDirs:= 0;
  680.   FindFirst (dirName, AnyFile, FileInfo);
  681.   while DosError = 0 do
  682.     begin
  683.       if ((FileInfo.Attr and VolumeId) = 0) then
  684.         if (FileInfo.Attr and Directory) > 0 then
  685.           if (FileInfo.Name <> '.') and (FileInfo.Name <> '..') then
  686.             Inc (nberOfDirs);
  687.       FindNext (FileInfo);
  688.     end; {while}
  689.   ddirsfn := nberOfDirs;
  690. end;  (* ddirsfn *)
  691. --------------------------------------------------------------------
  692.  
  693. From ts@uwasa.fi Wed Aug 18 00:00:13 1993
  694. Subject: A generic power function code for TP
  695.  
  696. 13. *****
  697.  Q: I need a power function but there is none in Turbo Pascal.
  698.  
  699.  A: Pascals do not have an inbuilt power function. You have to write
  700. one yourself. The common, but non-general method is defining
  701.    function POWERFN (number, exponent : real) : real;
  702.      begin
  703.        powerfn := Exp(exponent*Ln(number));
  704.      end;
  705. To make it general use:
  706.    (* Generalized power function by Prof. Timo Salmi *)
  707.    function GENPOWFN (number, exponent : real) : real;
  708.    begin
  709.      if (exponent = 0.0) then
  710.        genpowfn := 1.0
  711.      else if number = 0.0 then
  712.        genpowfn := 0.0
  713.      else if abs(exponent*Ln(abs(number))) > 87.498 then
  714.        begin writeln ('Overflow in GENPOWFN expression'); halt; end
  715.      else if number > 0.0 then
  716.        genpowfn := Exp(exponent*Ln(number))
  717.      else if (number < 0.0) and (Frac(exponent) = 0.0) then
  718.        if Odd(Round(exponent)) then
  719.          genpowfn := -GENPOWFN (-number, exponent)
  720.        else
  721.          genpowfn :=  GENPOWFN (-number, exponent)
  722.      else
  723.        begin writeln ('Invalid GENPOWFN expression'); halt; end;
  724.    end;  (* genpowfn *)
  725. --------------------------------------------------------------------
  726.  
  727. From ts@uwasa.fi Wed Aug 18 00:00:14 1993
  728. Subject: Arrays > 64K
  729.  
  730. 14. *****
  731.  Q: How can I create arrays that are larger than 64 kilobytes?
  732.  
  733.  A: Turbo Pascal does not directly support the so-called huge arrays
  734. but you can get by this problem with a clever use of pointers as
  735. presented in Kent Porter, "Handling Huge Arrays", Dr.Dobb's Journal,
  736. March 1988. In this method you point to an element of a two
  737. dimensional array using a^[row].col^[column]. The idea involves too
  738. much code and explanation to be repeated here, so you'll have to see
  739. the original reference. But I know from my own experience, that the
  740. code works like magic. (The code is available from garbo.uwasa.fi
  741. archives as /pc/turbopas/ddj8803.zip). Kent Porter, "Huge Arrays
  742. Revisited", Dr.Dobb's Journal, October 1988, presents the extension
  743. of the idea to huge virtual arrays. (Virtual arrays mean arrays that
  744. utilize disk space).
  745.    Another possibility is using TurboPower Software's (the usual
  746. disclaimer applies) commercial Turbo Professional (don't confuse
  747. with Borland's Turbo Professional) package. It has facilities for
  748. huge arrays, but they involve much more overhead than Kent Porter's
  749. excellent method.
  750.  
  751. (* =================================================================
  752.    My code below is based on a UseNet posting in comp.lang.pascal
  753.    by Naji Mouawad nmouawad@watmath.waterloo.edu. Naji's idea was
  754.    for a vector, my adaptation is for a two-dimensional matrix. The
  755.    realization of the idea is simpler than the one presented by Kent
  756.    Porter in Dr.Dobb's Journal, March 1988. (Is something wrong,
  757.    this is experimental.)
  758.    ================================================================= *)
  759.    {}
  760.    const maxm = 150;
  761.          maxn = 250;
  762.    {}
  763.    type BigVectorType = array [1..maxn] of real;
  764.         BigMatrixType = array [1..maxm] of ^BigVectorType;
  765.    {}
  766.    var BigAPtr : BigMatrixType;
  767.    {}
  768.    (* Create the dynamic variables *)
  769.    procedure MAKEBIG;
  770.    var i          : word;
  771.        heapNeeded : longint;
  772.    begin
  773.      heapNeeded := maxm * maxn * SizeOf(real) + maxm * 4 + 8196;
  774.      if (MaxAvail <= heapNeeded) then
  775.        begin writeln ('Out of memory'); halt; end;
  776.      for i := 1 to maxm do New (BigAPtr[i]);
  777.    end;  (* makebig *)
  778.    {}
  779.    (* Test that it works *)
  780.    procedure TEST;
  781.    var i, j : word;
  782.    begin
  783.      for i := 1 to maxm do
  784.        for j := 1 to maxn do
  785.          BigAPtr[i]^[j] := i * j;
  786.      {}
  787.      writeln (BigAPtr[5]^[7] : 0:0);
  788.      writeln (BigAPtr[maxm]^[maxn] : 0:0);
  789.    end;  (* test *)
  790.    {}
  791.    (* The main program *)
  792.    begin
  793.      writeln ('Big arrays test by Prof. Timo Salmi, Vaasa, Finland');
  794.      writeln;
  795.      MAKEBIG;
  796.      TEST;
  797.    end.
  798. (For a better test of the heap than in MAKEBIG see Swan (1989), pp.
  799. 462-463.)
  800. --------------------------------------------------------------------
  801.  
  802. From ts@uwasa.fi Wed Aug 18 00:00:15 1993
  803. Subject: Testing printer status
  804.  
  805. 15. *****
  806.  Q: How can I test that the printer is ready?
  807.  
  808.  A: The usually advocated method in Turbo Pascal is to test the
  809. status of regs.ah after a call to interrupt 17 Hex (the parallel
  810. port driver interrupt), service 02:
  811.       regs.dx := PrinterNumber;  (* LPT1 = 0 *)
  812.       regs.ah := $02;            (* var regs : registers, uses DOS *)
  813.       Intr ($17,regs);           (* Interrupt 17 Hex *)
  814.       status := regs.ah          (* var status : byte *)
  815. But this is not a good method since the combinations of the status
  816. bits which indicate a ready state can vary from printer to printer
  817. and PC to PC. If you want a list of the status bits, see eg Ray
  818. Duncan (1988), Advanced MS-DOS Programming, p. 587. For an example
  819. of a code using interrupt 17 Hex see Douglas Stivison (1986), Turbo
  820. Pascal Library, pp. 118-120. Also see Michael Yester (1989), Using
  821. Turbo Pascal, pp. 494-495.
  822.    The more generic alternative is to try to write a #13 to the
  823. printer having the i/o checking off, that is, while {$I-} is in
  824. effect, and testing the IOResult. But then you must first alter the
  825. printer retry times default (and restore it afterwards). Else the
  826. method can take up to a minute instead of an immediate response.
  827. Also, you must have set the FileMode for LPT1 appropriately (and
  828. restore it afterwards). Sounds a bit complicated, but you don't have
  829. to do all this yourself. There is a boolean function "LPTONLFN Get
  830. the online status of the first parallel printer" for this purpose in
  831. my /pc/ts/tspa33??.zip (or whatever version number is the latest)
  832. Turbo Pascal units collection available by anonymous FTP or mail
  833. server from garbo.uwasa.fi.
  834. --------------------------------------------------------------------
  835.  
  836. From ts@uwasa.fi Wed Aug 18 00:00:16 1993
  837. Subject: Clearing the keyboard buffer
  838.  
  839. 16. *****
  840.  Q: How can I clear the keyboard type-ahead buffer.
  841.  
  842.  A: Three methods are usually suggested for solving this problem.
  843. a) The first is to use something like
  844.      uses Crt;
  845.      var dummy : char;
  846.      while KeyPressed do dummy := ReadKey;
  847. This kludge-type method has the disadvantage of requiring the Crt
  848. unit. Also, in connection with procedures relying on ReadKey for
  849. input, it may cause havoc on the programs logic.
  850. b) The second method accesses directly the circular keyboard buffer
  851.      var head : word absolute $0040:$001A;
  852.          tail : word absolute $0040:$001C;
  853.      procedure FLUSHKB; begin head := tail; end;
  854. c) The third method is to call interrupt 21Hex (the MsDos interrupt)
  855. with the ax register set as $0C00. This method has the advantage of
  856. not being "hard-coded" like the second method, and thus should be
  857. less prone to incompatibility.
  858. --------------------------------------------------------------------
  859.  
  860. From ts@uwasa.fi Wed Aug 18 00:00:17 1993
  861. Subject: Utilizing expanded memory
  862.  
  863. 17. *****
  864.  Q: How can I utilize expanded memory (EMS) in my programs?
  865.  
  866.  A: I have no experience (yet?) on this subject myself, but I can
  867. give you a list of references: Michael Tischer (1990), Turbo Pascal
  868. Internals, Abacus, Chapter 9; Stephen O'Brien (1988), Turbo Pascal,
  869. Advanced Programmer's Guide, Borland-Osborne, Chapter 4; Chris
  870. Ohlsen & Gary Stoker (1989), Turbo Pascal Advanced Techniques, Que,
  871. Chapter 11, and, maybe most importantly, Dorfman & Neuberger, Turbo
  872. Pascal Memory Management Techniques (with lots of code).
  873.    Furthermore, Turbo Pascal delivery disks (at least 5.0) contain a
  874. demos.arc archive which includes a ems.pas file.
  875. --------------------------------------------------------------------
  876.  
  877. From ts@uwasa.fi Wed Aug 18 00:00:18 1993
  878. Subject: Capturing the entire command line
  879.  
  880. 18. *****
  881.  Q: How can I obtain the entire command line (spaces and all)?
  882.  
  883.  A: ParamCount and ParamStr are for parsed parts of the command line
  884. and cannot be used to get the command line exactly as it was. See
  885. what happens if you try to capture
  886.   "Hello.   I'm here"
  887. you'll end up with a false number of blanks. For obtaining the
  888. command line unaltered use
  889.   type CommandLineType = string[127];
  890.   var  CommandLinePtr  : ^CommandLineType;
  891.   begin
  892.     CommandLinePtr := Ptr(PrefixSeg, $80);
  893.     writeln (CommandLinePtr^);
  894.   end;
  895. A warning. If you want to get this correct (the same goes for TP's
  896. own ParamStr and ParamCount) apply them early in your program. At
  897. least they must be used before any disk I/O takes place!
  898. --------------------------------------------------------------------
  899.  
  900. From ts@uwasa.fi Wed Aug 18 00:00:19 1993
  901. Subject: Redirecting from printer to file
  902.  
  903. 19. *****
  904.  Q: How do I redirect text from printer to file in my TP program?
  905.  
  906.  A: Simple. This is done in Turbo Pascal by using the assign command
  907. (think what the word 'assign' implies). Here is a simple example of
  908. the idea.
  909.   uses Printer;
  910.   begin
  911.     assign (lst, 'printer.log');
  912.     rewrite (lst);
  913.     writeln (lst, 'Hello world');
  914.     close (lst);
  915.   end.
  916. --------------------------------------------------------------------
  917.  
  918. From ts@uwasa.fi Wed Aug 18 00:00:20 1993
  919. Subject: Turbo Pascal users are just wimps
  920.  
  921. 20. *****
  922.  Q: Turbo Pascal is for wimps. Why don't you use standard Pascal or
  923. better still why don't you use C?
  924.  
  925.  A: These kinds of "real-programmers" statements often reflect what
  926. is called self-over-others attitude, and they are a part of a kind
  927. of a programming lore or cult. Basically, these attitudes waive the
  928. simple fact that one should select one's tools according to the task
  929. at hand, not vice versa. On the other hand one's productivity is
  930. usually best when being able to use tools which one is familiar and
  931. comfortable with. (Note however that the real-programmer's lore is
  932. not really interested in producing results.)
  933.    In very rough terms there are two attitudes to programming
  934. languages. They can be seen as tools for writing applications, or
  935. (by surprisingly many) as ends themselves.
  936.    If we first look at standard Pascal (versus Turbo Pascal),
  937. considering the language primary and its usage secondary is common.
  938. This results from the history of Pascal, since as we all know it was
  939. originally meant as a means for teaching programming concepts, not
  940. at all for writing applications. But because Pascal turned out to be
  941. useful also for writing applications, it has been extended for some
  942. operating systems, most notably MsDos (Turbo Pascal) and VAX/VMS
  943. (VAX Pascal). Both remedy a lot of flaws from the application
  944. programmer's point of view. Most notably they have a true file I/O
  945. interface, and enhanced string handling. Turbo Pascal (the more
  946. generic of these two) clearly draws from the structure and ideas of
  947. advanced BASICs (and vice versa). While in standard Pascal the
  948. language is an end by itself, for Turbo Pascal the only relevant
  949. issue is its usefulness for writing applications.
  950.    One problem that one encounters when moving away from standard
  951. Pascal is the problem of portability. This is a truly serious
  952. problem, since most often extensive rewriting is necessary from
  953. converting say a Turbo Pascal to, say, Unix Pascal. I have taken
  954. Unix Pascal as the extreme example, since Unix Pascal in almost
  955. nothing but the standard Pascal having no useful file I/O.
  956.    If one considers C, its best aspect from applications point of
  957. view is portability, and its strength for system programming. But it
  958. is not an easy language to learn. Proponents of C also often have
  959. the tendency discussed above, that is seeing the language as
  960. primary, and its utilization as secondary. Now why this tendency,
  961. not only for C, but in general? I've had the opportunity of writing
  962. programs starting from the late 1960's, and one observation I have
  963. made, and often propounded the view is that it is not writing code
  964. that is the really difficult part. What is really difficult it is
  965. coming up with good and original ideas for programs to write. I see
  966. applications as primary, and the tools as secondary. As to Turbo
  967. Pascal, I've written in many languages (including Cobol, Fortran,
  968. several Basics and Pascals, and command languages) and I like Turbo
  969. Pascal because it is one of the most convenient and flexible tools
  970. for writing the kind of applications that I usually write and
  971. distribute for the Public Domain. That is I use Turbo Pascal because
  972. I'm comfortable with it in writing applications, and have thus
  973. gathered a very useful modular libary for it over the years, not
  974. because of any inherent value attached to Turbo Pascal per se.
  975.  
  976.  A2: Another, a somewhat resembling line is made up by the arguments
  977. about standards in Pascal which are recycled in comp.lang.pascal
  978. time after time. Very often they end up with purists vs pragmatists
  979. arguing about the true or imaginary viles of using GOTOs. I find all
  980. this somewhat futile, although I understand the academic nature of
  981. the background. As you'll recall, Pascal was first developed for
  982. academic teaching programming concepts, not for any practical
  983. programming. That came later, and the ensuing popularity of Pascal
  984. in practical applications must have come as a surprise way back
  985. then. I admit being biased in not symphatizing with Pascal standard
  986. stalwarts. I am far more interested in getting the job done than in
  987. defending a barren orthodoxy.
  988. --------------------------------------------------------------------
  989.  
  990. From ts@uwasa.fi Wed Aug 18 00:00:21 1993
  991. Subject: Turning off the cursor
  992.  
  993. 21. *****
  994.  Q: How do I turn the cursor off?
  995.  
  996.  A: The usually advocated trick for turning the cursor off is to
  997. equate the lower and the upper scan line of the cursor as explained
  998. eg in Stephen O'Brien (1988), Turbo Pascal, Advanced Programmer's
  999. Guide.
  1000.      uses Dos;
  1001.      var regs : registers;
  1002.      begin
  1003.        regs.ax := $0100;   (* Service $01 *)
  1004.        regs.cl := $20;     (* Top scan line *)
  1005.        regs.ch := $20;     (* Botton scan line *)
  1006.        Intr ($10, regs);   (* ROM BIOS video driver interrupt *)
  1007.      end;
  1008. To turn the cursor back on this (and many other) sources suggest
  1009. setting regs.ch and regs.cl as 12 and 13 for mono screen, and 6 and
  1010. 7 for others.
  1011.    This is not a good solution since it is equipment dependent, and
  1012. may thus produce unexcepted results. Better to store the current
  1013. scan line settings, and turn off the cursor bit. Below is the code
  1014. from my Turbo Pascal units collection /pc/ts/tspa33??.zip (or
  1015. whatever version number is the latest) available by anonymous FTP
  1016. from garbo.uwasa.fi archives. The general idea is that regs.ch bit 5
  1017. toggles the cursor on / off state. Thus to set the cursor off, apply
  1018.   regs.ch := regs.ch or $20;    (* $20 = 00100000 *)
  1019. and to set it on, apply
  1020.   regs.ch := regs.ch and $DF;   (* $DF = 11011111 *)
  1021. (* From TSUNTE unit, which also has a CURSON procedure *)
  1022. procedure CURSOFF;
  1023. var regs : registers;
  1024. begin
  1025.   FillChar (regs, SizeOf(regs), 0);  (* Initialize, a precaution *)
  1026.   {... find out the current cursor size (regs.ch, regs.cl) ...}
  1027.   regs.ah := $03;
  1028.   regs.bh := $00;    (* page 1, superfluous because of FillChar *)
  1029.   Intr ($10, regs);  (* ROM BIOS video driver interrupt *)
  1030.   {... turn off the cursor without changing its size ...}
  1031.   regs.ah := $01;                   (* Below are bits 76543210 *)
  1032.   regs.ch := regs.ch or $20;  (* Turn on bit 5; $20 = 00100000 *)
  1033.   Intr ($10, regs);
  1034. end;  (* cursoff *)
  1035.  
  1036.  A2: Another solution that has been suggested is putting the cursor
  1037. outside the screen using the GoToXY procedure. Fair enough, but then
  1038. you need to use the Crt unit, which is not always desirable.
  1039. Besides, how do you write on the screen if the cursor postion is off
  1040. it?
  1041.  
  1042.  A3: (Not to be taken seriously). Simple, turn off your computer and
  1043. the cursor stops showing :-).
  1044. --------------------------------------------------------------------
  1045.  
  1046. From ts@uwasa.fi Wed Aug 18 00:00:22 1993
  1047. Subject: Finding the roots of a polynomial
  1048.  
  1049. 22. *****
  1050.  Q: How to find all roots of a polynomial?
  1051.  
  1052.  A: If you need the code, see Turbo Pascal Numerical Toolbox and/or
  1053. Press & Flannery & Teukolsky & Vetterling (1986), Numerical Recipes,
  1054. The Art of Scientific Computing, Cambridge University Press. The
  1055. Numerical Recipes codes are available as /pc/turbopas/nrpas13.zip
  1056. (big, 404k!). If you just need to solve such a task (without code
  1057. available), get /pc/ts/tsnum12.zip (or whatever version number is
  1058. the latest) from garbo.uwasa.fi archives by anonymous FTP or mail
  1059. server.
  1060. --------------------------------------------------------------------
  1061.  
  1062. From ts@uwasa.fi Wed Aug 18 00:00:23 1993
  1063. Subject: Pascal homework on the net
  1064.  
  1065. 23. *****
  1066.  Q: What is all this talk about "Pascal homework on the net"?
  1067.  
  1068.  A: This is one of the subjects that seems to pop up at regular
  1069. intervals, cause some heated exchange for awhile, and then die down
  1070. again leaving some users harboring warranted or unwarranted grudges.
  1071.    Some posters to comp.lang.pascal have been very concerned of the
  1072. possibility that the questions posed on the net are related to
  1073. students' homework assignments. I don't have any unequivocal answers
  1074. or a clearcut stand on this question, just some comments.
  1075.    The most important task of a newsgroup like comp.lang.pascal is
  1076. the exchange of information between the users. If you think that
  1077. what you are going to post is interesting and useful to the group,
  1078. that should be your topmost criterion.
  1079.    If it is really a student that wants his/her work done on the net
  1080. (how do we know anyway?) also consider the following fact. Being
  1081. able to use a newsgroup amounts to having learned at least something
  1082. about using computers, and that is something per se.
  1083.    Even if the student may short-sightedly not see it, providing ALL
  1084. the code for a student's homework is detrimental to the student,
  1085. since it is she/he that foregoes understanding what he/she is doing.
  1086. The group should not condone outright cheating. Being (partly) a
  1087. teacher myself, I understand also this view.
  1088.    If a student is stuck with a problem in his/her code, I don't see
  1089. any real harm in helping out, especially if the problem has general
  1090. interest. Instructing is what teaching is about, anyway, isn't it?
  1091.    But, on the other hand, I must admit that I find a it rather
  1092. flagrant if a posting asks for something of the kind "I have to
  1093. complete my term assignment to write a function plotter by the end
  1094. of this month. Send me the code, since I'm too busy with my other
  1095. exams to write it myself" (a true quote).
  1096.    Finally, let's not jump to premature conclusions about anyone's
  1097. questions.  That's what most often triggers off a vicious circle of
  1098. flaming.
  1099. --------------------------------------------------------------------
  1100.  
  1101. From ts@uwasa.fi Wed Aug 18 00:00:24 1993
  1102. Subject: Linking bgi drivers into executables
  1103.  
  1104. 24. *****
  1105.  Q: How can I link graphics drivers directly into my executable?
  1106.  
  1107.  A: This is a complicated, yet a very useful task, because then you
  1108. won't need any separate graphics drivers (or fonts) to go separately
  1109. along with your program. Unfortunately, Turbo Pascal documentation
  1110. on this task is a bit confusing.
  1111.    1) The very first step is to get the necessary files from the
  1112. Turbo Pascal disks to your working directory. To start with, you'll
  1113. need binobj.exe and all the .bgi files.
  1114.    2) Run the following commands (best to place them in a batch,
  1115. call it eg makeobj.bat):
  1116.      binobj cga.bgi cga CGADriverProc
  1117.      binobj egavga.bgi egavga EGAVGADriverProc
  1118.      binobj herc.bgi herc HercDriverProc
  1119.      binobj pc3270.bgi pc3270 PC3270DriverProc
  1120.      binobj att.bgi att ATTDriverProc
  1121.      rem binobj ibm8514.bgi 8514 IBM8514DriverProc
  1122.    3) Get drivers.pas from the Turbo Pascal disk and compile it with
  1123. Turbo Pascal. Now you have a drivers.tpu unit which contains all the
  1124. graphics drivers.
  1125.    4) Now you won't need the .bgi and the .obj files any more. You
  1126. may delete them from your working directory.
  1127.    5) Write your graphics program in the usual manner. But before
  1128. putting your program in the graphics mode use the following
  1129. procedure if you want to link e.g. the EGAVGA graphics driver
  1130. directly into your executable. (Link just the driver(s) you'll need,
  1131. since the drivers take up a lot of space.)
  1132.      uses Graph, Drivers;
  1133.      :
  1134.      procedure EGAVGA2EXE;
  1135.      begin
  1136.        if RegisterBGIdriver(@EGAVGADriverProc) < 0 then
  1137.          begin
  1138.            writeln ('EGA/VGA: ', GraphErrorMsg(GraphResult));
  1139.            halt(1);
  1140.          end;
  1141.      end; (* egavga2exe *)
  1142.      :
  1143.    Incidentally, although this is a slightly different matter, you
  1144. can link any data material into your executable. See Stephen
  1145. O'Brien, (1988), Turbo Pascal, Advanced Programmer's Guide, pp. 31 -
  1146. 35 for more details.
  1147. --------------------------------------------------------------------
  1148.  
  1149. From ts@uwasa.fi Wed Aug 18 00:00:25 1993
  1150. Subject: Trapping runtime errors
  1151.  
  1152. 25. *****
  1153.  Q: How can I trap a runtime error?
  1154.  
  1155.  A: What you are probably asking for is a method writing a program
  1156. termination routine of your own. To do this, you have to replace
  1157. Turbo Pascal's ExitProc with your own customized exec procedure.
  1158. Several Turbo Pascal text books show ho to do this. See eg Tom Swan
  1159. (1989), Mastering Turbo Pascal 5.5, Third edition, Hayden Books, pp.
  1160. 440-454; Michael Yester (1989), Using Turbo Pascal, Que, pp.
  1161. 376-382; Stephen O'Brien (1988), Turbo Pascal, Advanced Programmer's
  1162. Guide, pp. 28-30; Tom Rugg & Phil Feldman (1989), Turbo Pascal
  1163. Programmer's Toolkit, Que, pp. 510-515. Here is an example
  1164.   var OldExitProcAddress : Pointer;
  1165.       x : real;
  1166.   {$F+} procedure MyExitProcedure; {$F-}
  1167.   begin
  1168.     if ErrorAddr <> nil then
  1169.       begin
  1170.         writeln ('Runtime error number ', ExitCode, ' has occurred');
  1171.         writeln ('The error address in decimal is ',
  1172.                   Seg(ErrorAddr^):5,':',Ofs(ErrorAddr^):5);
  1173.         writeln ('That''s all folks, bye bye');
  1174.         ErrorAddr := nil;
  1175.         ExitCode  := 0;
  1176.       end;
  1177.     {... Restore the pointer to the original exit procedure ...}
  1178.     ExitProc := OldExitProcAddress;
  1179.   end;  (* MyExitProcedure *)
  1180.   (* Main *)
  1181.   begin
  1182.     OldExitProcAddress := ExitProc;
  1183.     ExitProc := @MyExitProcedure;
  1184.     x := 7.0; writeln (1.0/x);
  1185.     x := 0.0; writeln (1.0/x);   {The trap}
  1186.     x := 7.0; writeln (4.0/x);   {We won't get this far}
  1187.   end.
  1188. :
  1189. Actually, I utilize this idea in my /pc/ts/tspa33??.zip Turbo Pascal
  1190. units collection, which includes a TSERR.TPU. If you put TSERR in
  1191. your program's uses statement, all the run time errors will be given
  1192. verbally besides the usual, cryptic error number. That's all there
  1193. is to it. That is, the inclusion to the uses statment to your main
  1194. program (if you have the program in several units) is all you have
  1195. to do to enable this handy feature.
  1196. --------------------------------------------------------------------
  1197.  
  1198. From ts@uwasa.fi Wed Aug 18 00:00:26 1993
  1199. Subject: Using ansi codes in a TP program
  1200.  
  1201. 26. *****
  1202.  Q: How to get ansi control codes working in Turbo Pascal writes?
  1203.  
  1204.  A: It is very simple, but one has to be aware of the pitfalls.
  1205. Let's start from the assumption that ansi.sys or a corresponding
  1206. driver has been loaded, and that you know ansi codes. If you don't,
  1207. you'll find that information in the standard MsDos manual. To apply
  1208. ansi codes you just include the ansi codes in your write statements.
  1209. For example the following first clears the screen and then puts the
  1210. text at location 10,10:
  1211.    write (#27, '[2J');         (* the ascii code for ESC is 27 *)
  1212.    write (#27, '[10;10HUsing ansi codes can be fun');
  1213. If you want to test (as you should) whether ansi.sys or some some
  1214. replacement driver has been loaded, you can use the ISANSIFN
  1215. function from my garbo.uwasa.fi:/pc/ts/tspa33??.zip.
  1216. Now the catches. If you have a
  1217.    uses Crt;
  1218. statement in your program, direct screen writes will be used, and
  1219. the ansi codes won't work. You have either to leave out the Crt
  1220. unit, or include
  1221.    assign (output, '');
  1222.    rewrite (output);
  1223.    :
  1224.    close (output);
  1225. Occasionally I have seen it suggested that one should just set
  1226.    DirectVideo := false;
  1227. This is a popular misconception. It won't produce the desired
  1228. result. I'm not claiming to know the reason for this quirk of Turbo
  1229. Pascal. Rather it is an observation I've made.
  1230.  
  1231. -From: Bengt Oehman (d92bo@efd.lth.se)
  1232. The `DirectVideo:=False' statement only tells the Crt unit to use
  1233. BIOS calls instead of using direct video-memory writes. A demo
  1234. program to illustrate the screen writing modes follows:
  1235.  
  1236. Program ScreenWriteDemo;
  1237. USES Crt;
  1238. BEGIN
  1239.   Writeln('This is written directly to the video memory');
  1240.   DirectVideo:=False;
  1241.   Writeln('This is written via BIOS interrupt calls (int 10h)');
  1242.   Assign(Output,'');
  1243.   Append(Output);
  1244.   Writeln('This is written via DOS calls (int 21h)');
  1245. END.
  1246. --------------------------------------------------------------------
  1247.  
  1248. From ts@uwasa.fi Wed Aug 18 00:00:27 1993
  1249. Subject: Writing an expression parser
  1250.  
  1251. 27. *****
  1252.  Q: How to evaluate a function given as a string to the program?
  1253.  
  1254.  A: To do this you have to have a routine for parsing and evaluating
  1255. your expression. This is a complicated task requiring a clever use
  1256. of recursion. You can find such code in Stephen O'Brien (1988),
  1257. Turbo Pascal, The Complete Reference. Borland-Osborne/McGraw-Hill,
  1258. Chapter 10. Another, simpler piece of code can be found in Michael
  1259. Yester (1989), Using Turbo Pascal, Que, Chapter 5.
  1260.    I've also written such a function evaluation program myself, and
  1261. much of it is based on the ideas in O'Brien with my own corrections
  1262. and enhancements. The resulting program is available as fn.exe
  1263. function evaluator in the /pc/ts/tsfunc13.zip package (or whatever
  1264. version number is the latest). Note however, that the source code is
  1265. not included, nor available.
  1266.    Tips from Justin Lee (ossm1jl@rex.uokhsc.edu):
  1267. 9158 Apr 25 1992 garbo.uwasa.fi:/pc/turbopas/parse11.zip
  1268. parse11.zip Recursive expression Turbo Pascal parser from Ron Loewy
  1269. An excellent parser is included with all the Turbo Pascal versions
  1270. since TP4.0 as part of the MCALC or TCALC spreadsheet example
  1271. program. See mcparse.pas or tcparse.pas.
  1272. --------------------------------------------------------------------
  1273.  
  1274. From ts@uwasa.fi Wed Aug 18 00:00:28 1993
  1275. Subject: Detecting redirection
  1276.  
  1277. 28. *****
  1278.  Q: How does one detect whether input (or output) is redirected?
  1279.  
  1280.  A: As we know input to a program can come from a file, from the
  1281. console, or from a pipe or redirection. Examples of the latter are
  1282.      type text.dat | program
  1283.      program < text.dat
  1284. A Turbo Pascal program can be made to detect the redirections using
  1285. Interrupt 21Hex, function 44Hex, subfunction 00Hex. See PC Magazine
  1286. April 16, 1991, p. 374 for the code, and Duncan (1988), Advanced
  1287. MS-DOS Programming, pp. 412-413 for more information. Alternatively,
  1288. you can utilize the preprogrammed routines
  1289.   PIPEDIFN Is the standard input from redirection
  1290.   PIPEDNFN Is the standard output redirected to nul
  1291.   PIPEDOFN Is the standard output redirected
  1292. from my garbo.uwasa.fi:/pc/ts/ tspa33??.zip units.
  1293. --------------------------------------------------------------------
  1294.  
  1295. From ts@uwasa.fi Wed Aug 18 00:00:29 1993
  1296. Subject: Setting the 43/50 line text mode
  1297.  
  1298. 29. *****
  1299.  Q: How does one set the 43/50 line text mode?
  1300.  
  1301.  A: Quite simple. Just apply TextMode (C80 + font8x8).  Requires a
  1302. "uses Crt;". First, however, you should test that you have a at
  1303. least an EGA video adapter. (See DetectGraph in your TP manual).
  1304. Also see TSUTLE.NWS in garbo.uwasa.fi:/pc/ts/tsutle22.zip (or
  1305. whichever version number is the current) for the non-standard wide
  1306. text modes like 132x43.
  1307. --------------------------------------------------------------------
  1308.  
  1309. From ts@uwasa.fi Wed Aug 18 00:00:30 1993
  1310. Subject: Assigning environment variable values
  1311.  
  1312. 30. *****
  1313.  Q: How can I assign a value to an environment variable in TP?
  1314.  
  1315.  A: For assigning a value to (a parent process's) environment value
  1316. you have to access and manipulate the Program Segment Prefix and
  1317. Memory Control Blocks. This is a rather complicated undertaking. A
  1318. source code with an accompanying article by Trudy Neuhaus can be
  1319. found in PC Magazine Volume 11 Number 1 pages 425-427.
  1320.    The budding TP programmers should note that the elementary trick
  1321. of Exec (GetEnv('comspec'), '/c set key=whatever') will achieve only
  1322. a transient result for the duration of the exec shell. When you exit
  1323. the shell after this endeavor, the environment will be as it was.
  1324.    Here is about the why. When the above command is executed, MsDos
  1325. makes a copy of the environment, and uses the copy. When the above
  1326. shelling terminates, the copy of the environment is deleted, and the
  1327. original is restored. Hence the above trick cannot be used to change
  1328. the parent environment.
  1329.    If you don't want to try to go through this rather complicated
  1330. task yourself, the routines
  1331.  "SETENV   Set a parent environment variable (variable=value)"
  1332.  "SETENVSH Set an environment variable for the duration of shelling"
  1333. can be found in my TP TPU collection garbo.uwasa.fi:/pc/ts/
  1334. tspa33*.zip (* = 40,50,55,60,70). No source code is included, nor
  1335. available, though.
  1336. --------------------------------------------------------------------
  1337.